<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>子女关联管理</span>
        <div class="extra">
          <el-button type="primary" @click="showDialog()">新增子女关联</el-button>
        </div>
      </div>
    </template>
    <el-table :data="relevanceList" style="width: 100%">
      <el-table-column label="序号" width="50" type="index"></el-table-column>
      <el-table-column label="家长姓名" prop="patriarchName"></el-table-column>
      <el-table-column label="学生姓名" prop="studentName"></el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button icon="el-icon-delete" circle plain type="danger" @click="deleteRelevance(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <el-dialog v-model="dialogVisible" title="新增子女关联" width="30%">
      <el-form :model="relevanceModel" :rules="rules" label-width="100px">
        <el-form-item label="学生ID" prop="studentId">
          <el-input v-model="relevanceModel.studentId"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="AddRelevance()">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addRelevance, fetchRelevanceList, deleteRelevanceById } from '@/api/childService.js';

const relevanceList = ref([]);
const dialogVisible = ref(false);
const relevanceModel = ref({ studentId: '' });
const rules = {
  studentId: [{ required: true, message: '请输入学生ID', trigger: 'blur' }]
};

const fetchRelevances = async () => {
  try {
    const result = await fetchRelevanceList();
    if (result.code === 0 && result.data) {
      relevanceList.value = result.data;
    } else {
      ElMessage.error('未能正确获取关联列表数据');
    }
  } catch (error) {
    console.error('Fetch relevance list error:', error);
    ElMessage.error('获取关联列表失败');
  }
};

const AddRelevance = async () => {
  try {
    const result = await addRelevance(relevanceModel.value.studentId);
    if (result.code === 0) {
      ElMessage.success(result.msg || '添加成功');
      await fetchRelevances();
      dialogVisible.value = false;
    } else {
      ElMessage.error(result.msg || '添加失败');
    }
  } catch (error) {
    ElMessage.error('添加关联失败');
  }
};

const deleteRelevance = (row) => {
  ElMessageBox.confirm('你确认要解除该子女关联吗?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const result = await deleteRelevanceById(row.id);
      if (result.code === 0) {
        ElMessage.success(result.msg || '解除成功');
        await fetchRelevances();
      } else {
        ElMessage.error(result.msg || '解除失败');
      }
    } catch (error) {
      ElMessage.error('解除关联失败');
    }
  }).catch(() => {
    ElMessage.info('已取消解除');
  });
};

const showDialog = () => {
  dialogVisible.value = true;
  relevanceModel.value = { studentId: '' };
};

onMounted(() => {
  fetchRelevances();
});
</script>

<style scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
